package nflparsing;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;


//"Addai, Joseph","2613","IND","9001","RB","","1","1","0","0","0","0","0","0",
//"0","0","0","10","44","0","0","6","6","29","0","0","0","0","0","0","0","0",
//"0","0","0","0","0",""
public class WeekParser {
	
	public static void parseWeekOffensive(String filename, int weekNumber, HashMap<Integer, Player> playerTable) {

		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String line = "";

			while ((line = br.readLine()) != null) {
				int startIndex = 0;
				Vector<String> rez = new Vector<String>();
				
				while (true) {
					int endIndex = line.indexOf("\"", startIndex+1);
					if (endIndex > 0) {
						rez.add(line.substring(startIndex+1, endIndex));
						startIndex = line.indexOf("\"", endIndex+1);
					} else {
						break;
					}
				}
				
				if (rez.size() < 10) {
					return;
				}
				
				Player p = playerTable.get(Integer.valueOf(rez.get(1)));
				if (p == null) {
					p = new Player();
					p._fullName = rez.get(0);
					p._playerID = Integer.valueOf(rez.get(1));
					p._teamName = rez.get(2);
					p._teamID = Integer.valueOf(rez.get(3));
					p._position = rez.get(4);
				}
				p._fullString[weekNumber] =line;
				p._injury[weekNumber] = rez.get(5);
				p._started[weekNumber] = Boolean.valueOf(rez.get(6));
				p._played[weekNumber] = Boolean.valueOf(rez.get(7));
				p._inactive[weekNumber] = Boolean.valueOf(rez.get(8));
				p._passingCompletions[weekNumber] = Integer.valueOf(rez.get(9));
				p._passingAttempts[weekNumber] = Integer.valueOf(rez.get(10));
				p._passingYards[weekNumber] = Integer.valueOf(rez.get(11));
				p._passingInts[weekNumber] = Integer.valueOf(rez.get(12));
				p._passingTDs[weekNumber] = Integer.valueOf(rez.get(13));
				p._passingTwoPts[weekNumber] = Integer.valueOf(rez.get(14));
				p._sacked[weekNumber] = Integer.valueOf(rez.get(15));
				p._sackYardsLost[weekNumber] = Integer.valueOf(rez.get(16));
				p._rushingAttempts[weekNumber] = Integer.valueOf(rez.get(17));
				p._rushingYards[weekNumber] = Integer.valueOf(rez.get(18));
				p._rushingTDs[weekNumber] = Integer.valueOf(rez.get(19));
				p._rushingTwoPts[weekNumber] = Integer.valueOf(rez.get(20));
				p._targets[weekNumber] = Integer.valueOf(rez.get(21));
				p._receivingReceptions[weekNumber] = Integer.valueOf(rez.get(22));
				p._receivingYards[weekNumber] = Integer.valueOf(rez.get(23));
				p._receivingTDs[weekNumber] = Integer.valueOf(rez.get(24));
				p._receivingTwoPts[weekNumber] = Integer.valueOf(rez.get(25));
				p._kickingPATMade[weekNumber] = Integer.valueOf(rez.get(26));
				p._kickingPATAttempt[weekNumber] = Integer.valueOf(rez.get(27));
				p._kickingPATBlocked[weekNumber] = Integer.valueOf(rez.get(28));
				p._kickingFGMade[weekNumber] = Integer.valueOf(rez.get(29));
				p._kickingFGAttempt[weekNumber] = Integer.valueOf(rez.get(30));
				p._kickingFGBlocked[weekNumber] = Integer.valueOf(rez.get(31));
				p._kickingFGM_1_29[weekNumber] = Integer.valueOf(rez.get(32));
				p._kickingFGM_30_39[weekNumber] = Integer.valueOf(rez.get(33));
				p._kickingFGM_40_49[weekNumber] = Integer.valueOf(rez.get(34));
				p._kickingFGM_50p[weekNumber] = Integer.valueOf(rez.get(35));
				p._fumblesLost[weekNumber] = Integer.valueOf(rez.get(36));
				p._TDsByDist[weekNumber] = rez.get(37);
				//p._TDsByDist2[weekNumber] = rez.get(37);
					
				playerTable.put(p._playerID, p);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public static void parseWeekSpecial(String filename, int weekNumber, HashMap<Integer, Player> playerTable) {

		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String line = "";

			while ((line = br.readLine()) != null) {
				int startIndex = 0;
				Vector<String> rez = new Vector<String>();
				while (true) {
					int endIndex = line.indexOf("\"", startIndex+1);
					if (endIndex > 0) {
						rez.add(line.substring(startIndex+1, endIndex));
						startIndex = line.indexOf("\"", endIndex+1);
					} else {
						break;
					}
				}
				
				if (rez.size() < 10) {
					return;
				}
				
				Player p = playerTable.get(Integer.valueOf(rez.get(1)));
				if (p == null) {
					p = new Player();
					p._fullName = rez.get(0);
					p._playerID = Integer.valueOf(rez.get(1));
					p._teamName = rez.get(2);
					p._teamID = Integer.valueOf(rez.get(3));
					p._position = rez.get(4);
				}
				if (p._fullString[weekNumber] == null) {
					p._fullString[weekNumber] = line;
				}
				p._injury[weekNumber] = rez.get(5);
				p._started[weekNumber] = Boolean.valueOf(rez.get(6));
				p._played[weekNumber] = Boolean.valueOf(rez.get(7));
				p._inactive[weekNumber] = Boolean.valueOf(rez.get(8));
				p._puntReturns[weekNumber] = Boolean.valueOf(rez.get(9));
				p._puntReturnYards[weekNumber] = Integer.valueOf(rez.get(10));
				p._puntReturnFairCatch[weekNumber] = Integer.valueOf(rez.get(11));
				p._puntReturnTDs[weekNumber] = Integer.valueOf(rez.get(12));
				p._kickoffReturns[weekNumber] = Integer.valueOf(rez.get(13));
				p._kickoffReturnYards[weekNumber] = Integer.valueOf(rez.get(14));
				p._kickoffReturnFairCatch[weekNumber] = Integer.valueOf(rez.get(15));
				p._kickoffReturnTDs[weekNumber] = Integer.valueOf(rez.get(16));
				p._blockedKicks[weekNumber] = Integer.valueOf(rez.get(17));
				p._blockedKickRecoveries[weekNumber] = Integer.valueOf(rez.get(18));
				p._blockedKickReturnYards[weekNumber] = Integer.valueOf(rez.get(19));
				p._blockedKickReturnTDs[weekNumber] = Integer.valueOf(rez.get(20));
				p._fumblesLost[weekNumber] = Integer.valueOf(rez.get(21));
				p._fumblesForced[weekNumber] = Integer.valueOf(rez.get(22));
				p._fumblesRecovered[weekNumber] = Integer.valueOf(rez.get(23));
				p._fumblesReturnYards[weekNumber] = Integer.valueOf(rez.get(24));
				p._fumblesReturnTDs[weekNumber] = Integer.valueOf(rez.get(25));
				p._punts[weekNumber] = Integer.valueOf(rez.get(26));
				p._puntsYards[weekNumber] = Integer.valueOf(rez.get(27));
				p._puntsInside20[weekNumber] = Integer.valueOf(rez.get(28));
				p._puntsTouchback[weekNumber] = Integer.valueOf(rez.get(29));
				p._puntsBlocked[weekNumber] = Integer.valueOf(rez.get(30));
				p._kickoffs[weekNumber] = Integer.valueOf(rez.get(31));
				p._kickoffsEndZone[weekNumber] = Integer.valueOf(rez.get(32));
				p._kickoffsTouchBack[weekNumber] = Integer.valueOf(rez.get(33));
				//Offensive file has Special Teams TDs but not the reverse
				if ( p._TDsByDist[weekNumber] == null) {
					p._TDsByDist[weekNumber] = rez.get(34);
				}
//				if ( p._TDsByDist2[weekNumber] == null) {
//					p._TDsByDist2[weekNumber] = rez.get(35);
//				}
				
					
				playerTable.put(p._playerID, p);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	static final Comparator<Player> ESPN_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return p1._ESPN - p2._ESPN;
		}
	};
	
	static final Comparator<Player> ESPN_LATE_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return (int)Math.ceil(p1._ESPN_LATE_WEIGHT - p2._ESPN_LATE_WEIGHT);
		}
	};
	
	static final Comparator<Player> NATE_ESPN_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return (int)Math.ceil(p1._NATE_ESPN - p2._NATE_ESPN);
		}
	};
	
	static final Comparator<Player> NATE_ESPN_LATE_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return (int)Math.ceil(p1._NATE_ESPN_LATE_WEIGHT - p2._NATE_ESPN_LATE_WEIGHT);
		}
	};
	
	static final Comparator<Player> YAHOO_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return p1._ESPN - p2._ESPN;
		}
	};
	
	static final Comparator<Player> YAHOO_LATE_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return (int)Math.ceil(p1._ESPN_LATE_WEIGHT - p2._ESPN_LATE_WEIGHT);
		}
	};
	
	static final Comparator<Player> NATE_YAHOO_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return (int)Math.ceil(p1._NATE_ESPN - p2._NATE_ESPN);
		}
	};
	
	static final Comparator<Player> NATE_YAHOO_LATE_ORDER = new Comparator<Player>() {
		public int compare(Player p2, Player p1) {
			return (int)Math.ceil(p1._NATE_ESPN_LATE_WEIGHT - p2._NATE_ESPN_LATE_WEIGHT);
		}
	};
	
	static void printList(String header, List<Player> players, int count) {
		if (count == 0) {
			count = players.size();
		}
		
		System.out.println(header);
		for (int i=0; i<count; ++i) {
			System.out.println("" + i + ": " + players.get(i).smallString());
		}
	}
	
	public static void main(String [] args) {
		HashMap<Integer, Player> playerTable = new HashMap<Integer, Player>();
		
		for (int i=1; i<=17; ++i) {
			String spacer = (i > 9 ? "" : "0");
			WeekParser.parseWeekOffensive("data/offensive/10wk" + spacer + i + "co.csv", i-1, playerTable);
			WeekParser.parseWeekSpecial("data/specialTeams/10wk" + spacer + i + "cs.csv", i-1, playerTable);
		}
		
		System.out.println("mapSize = " + playerTable.size());
		List<Player> playerList = new ArrayList<Player>();
		for (int i : playerTable.keySet()) {
			playerTable.get(i).calculateScores();
			playerList.add(playerTable.get(i));
		}
		
		Collections.sort(playerList, ESPN_ORDER);
		printList("ESPN:", playerList, 20);
		Collections.sort(playerList, ESPN_LATE_ORDER);
		printList("\nESPN_LATE:", playerList, 20);
		Collections.sort(playerList, NATE_ESPN_ORDER);
		printList("\nNATE_ESPN:", playerList, 20);
		Collections.sort(playerList, NATE_ESPN_LATE_ORDER);
		printList("\nNATE_ESPN_LATE:",playerList, 20);
		Collections.sort(playerList, YAHOO_ORDER);
		printList("YAHOO:", playerList, 20);
		Collections.sort(playerList, YAHOO_LATE_ORDER);
		printList("\nYAHOO_LATE:", playerList, 20);
		Collections.sort(playerList, NATE_YAHOO_ORDER);
		printList("\nNATE_YAHOO:", playerList, 20);
		Collections.sort(playerList, NATE_YAHOO_LATE_ORDER);
		printList("\nNATE_YAHOO_LATE:",playerList, 20);
		
		System.out.println(playerTable.get(11345).smallString());
		System.out.println(playerTable.get(1926).smallString());
	}

}
